package com.mzj.saas.commons;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;

/**
 * @Author: wangrl
 * @Description: Oauth2Util
 * @Date: 16:32 2018/11/20
 * @Version: 1.0
 */
@Component
public class Oauth2Util {

    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;



    //获取Code的scope
    private static String SCOPE = "snsapi_userinfo";

    //第三方代【oauth2授权换取code的URL】
    //private static String GET_CODE_REQUEST = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect";
    //oauth2授权换取code的URL 不走第三方
    private static String GET_CODE_REQUEST = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
    
    //第三方代【通过code换取网页授权access_token】
    //  private static String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=%s&code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s";
    //不走三方平台
    private static String GET_ACCESS_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&code=%s&secret=%s&grant_type=authorization_code";
    //获取用户信息url                           
    private static String GET_USER_INFO = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
    /**
     * 获取页面code
     * @param backUrl
     * @param serviceNumAppId
     * @param state
     * @return
     */
   /* public  String getCodeRequest(String backUrl,String serviceNumAppId,String state){
        *//**
         * 参数（URL，服务号AppId，重定向URL，SCOPE，推广人id，第三方平台AppId）
         *//*
        return String.format(GET_CODE_REQUEST,serviceNumAppId,urlToU8(backUrl),SCOPE,state,getComponentAppId());
    }*/
    
    /**
       * 获取页面code不走三方平台
     * @param backUrl
     * @param serviceNumAppId
     * @param state
     * @return
     */
    public static  String getCodeRequest(String backUrl,String serviceNumAppId,String state){
       
        
        return String.format(GET_CODE_REQUEST,serviceNumAppId,urlToU8(backUrl),SCOPE,state);
    }
    
 
    /**
     * 获取用户信息url
     * @param accessToken
     * @param openId
     * @return
     */
    public static String getUserInfoUrl(String accessToken,String openId){
        return String.format(GET_USER_INFO,accessToken,openId);
    }


    /**
     * 获取第三方平台AppId
     * @return
     */
    public String getComponentAppId(){
        String Component = redisTemplate.opsForValue().get("WECHAT_THIRD_PARTY");
        JSONObject obj = JSONObject.parseObject(Component);
        return obj.getString("appid");
    }

    /**
     * 重定向URL 转码
     * @param str
     * @return
     */
    private static String urlToU8(String str){
        String result = str;
        try {
            result = URLEncoder.encode(str,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 获取页面token
     * @param code
     * @param serviceNumAppId
     * @return
     */
    public static String getAccessTokenUrl(String code,String serviceNumAppId,String secret){
        /**
         * 参数 （URL，服务号AppId，页面授权code）
         */
        return String.format(GET_ACCESS_TOKEN, serviceNumAppId, code, secret);
    }

    

}
